
    /**  
    * @Title: FeignClientsCustomInterceptor.java
    * @Package com.efast.cafe.framework.feign
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年9月20日
    * @version V1.0  
    */
    
package com.efast.cafe.framework.feign.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.efast.cafe.framework.feign.constant.FeignConstant;
import com.efast.cafe.util.ConfigUtil;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
    * @ClassName: FeignClientsCustomInterceptor
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年9月20日
    *
    */
@Configuration
public class FeignClientsCustomInterceptor implements RequestInterceptor{

	private static final String secretkey = ConfigUtil.getValue(FeignConstant.FEIGN_SECRET_KEY);
	  /* (非 Javadoc)
	    * 
	    * 
	    * @param arg0
	    * @see feign.RequestInterceptor#apply(feign.RequestTemplate)
	    */
	    
	@Override
	public void apply(RequestTemplate template) {
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();  
	    if (requestAttributes != null) {  
	    	 HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();  
	 	    Enumeration<String> headerNames = request.getHeaderNames();  
	 	    if (headerNames != null) {  
	 	      while (headerNames.hasMoreElements()) {  
	 	        String name = headerNames.nextElement();  
	 	        Enumeration<String> values = request.getHeaders(name);  
	 	        while (values.hasMoreElements()) {  
	 	          String value = values.nextElement();  
	 	          template.header(name, value);  
	 	        }  
	 	      }  
	 	    } 
	    }  
	    template.header(FeignConstant.FEIGN_SECRET_HEADER_KEY,secretkey);

	}
}
